using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using Models;
using IDAL;
using DAL;

namespace ITAM.Pages.Assets
{
    public class EditDeviceModel : PageModel
    {
        private IDeviceRecordService deviceRecordService;
        private IDeviceCategoryService deviceCategoryService;
        private IDepreciationRuleService depreciationRuleService;
        private IPurchasedChannelService purchasedChannelService;
        private IVendorService vendorService;

        public SelectList DeviceCategories { get; set; }
        public SelectList Vendors { get; set; }
        public SelectList PurchasedChannels { get; set; }
        public SelectList DepreciationRules { get; set; }

        [BindProperty]
        public DeviceRecord DeviceRecord { get; set; }


        public EditDeviceModel(IDeviceRecordService deviceRecordService, IDeviceCategoryService deviceCategoryService,
        IVendorService vendorService, IDepreciationRuleService depreciationRuleService, IPurchasedChannelService purchasedChannelService)
        {
            this.deviceRecordService = deviceRecordService;
            this.deviceCategoryService = deviceCategoryService;
            this.depreciationRuleService = depreciationRuleService;
            this.purchasedChannelService = purchasedChannelService;
            this.vendorService = vendorService;
        }


        public void OnGet(uint id)
        {
            var categoriesResult = deviceCategoryService.GetCategories();
            DeviceCategories = new SelectList(categoriesResult, "Id", "Name");

            var vendorResult = vendorService.GetVendors();
            Vendors = new SelectList(vendorResult, "Id", "Name");

            var purchasedChannelsRecult = purchasedChannelService.GetPurchasedChannels();
            PurchasedChannels = new SelectList(purchasedChannelsRecult, "Id", "Name");

            var depreciationRulesResult = depreciationRuleService.GetDepreciationRules();
            DepreciationRules = new SelectList(depreciationRulesResult, "Id", "Name");

            // 根据ID查询设备
            DeviceRecord = deviceRecordService.GetDeviceById(id);
        }

        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            DeviceRecord.PurchaseChannelId = DeviceRecord.PurchaseChannelId == null ? 0 : DeviceRecord.PurchaseChannelId;
            DeviceRecord.DepreciationRuleId = DeviceRecord.DepreciationRuleId == null ? 0 : DeviceRecord.DepreciationRuleId;
            DeviceRecord.Description = string.IsNullOrEmpty(DeviceRecord.Description) ? "" : DeviceRecord.Description;
            DeviceRecord.Ip = string.IsNullOrEmpty(DeviceRecord.Ip) ? "" : DeviceRecord.Ip;
            DeviceRecord.Mac = string.IsNullOrEmpty(DeviceRecord.Mac) ? "" : DeviceRecord.Mac;
            // DeviceRecord.Price = DeviceRecord.Price == null ? 0 : DeviceRecord.Price;
            DeviceRecord.Photo = string.IsNullOrEmpty(DeviceRecord.Photo) ? "" : DeviceRecord.Photo;
            DeviceRecord.UpdatedAt = DateTime.Now;

            try
            {
                await deviceRecordService.UpdateDevice(DeviceRecord);
            }
            catch
            {
                if (!deviceRecordService.DeviceExists(DeviceRecord.Id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            return RedirectToPage("./Device");
        }

    }
}